<template>
  <div>
    <el-card style="width: 500px;">
      <el-form label-width="120px" size="small" :model="form" :rules="rules" ref="pass">

        <el-form-item label="原密码" prop="passWord">
          <el-input v-model="form.passWord" autocomplete="off" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="form.newPassWord" autocomplete="off" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassWord" autocomplete="off" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
export default {
  name: "Password",
  data(){
    return{
      form: {},
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      rules: {
        passWord: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          { min: 5, message: '长度不少于5位', trigger: 'blur' }
        ],
        newPassWord: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 5, message: '长度不少于5位', trigger: 'blur' }
        ],
        confirmPassWord: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, message: '长度不少于5位', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.form.userName = this.user.userName
  },
  methods: {
    save() {
      this.$refs.pass.validate((valid) => {
        if (valid) {
          if (this.form.newPassWord !== this.form.confirmPassWord) {
            this.$message.error("2次输入的新密码不相同")
            return false
          }
          this.request.post("/user/rePassWord", this.form).then(res => {
            if (res.code === 200) {
              this.$message.success("修改成功")
              //重新登录一下
              this.$store.commit("resetSys")
            } else {
              this.$message.error(res.message)
            }
          })
        }
      })
    },
  }
}
</script>

<style scoped>
div{
  margin: 0 auto;
}
</style>